package GUI;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
/**
 * Class to personalize a Field to be a button with an Image
 * on background
 * @author ALS Telecom
 *
 */
public class MyCustomButton extends Field implements DrawStyle{
   private String _name;
	/**
    * currente Picture in Display
    */
   protected Bitmap _currentPicture;
   /**
    * image for "in focus"
    */
   protected Bitmap _onPicture; 
   /**
    * image for "not in focus"
    */
   private Bitmap _offPicture; 
   /**
    * image for "Selected and not in focus"
    */
   private Bitmap _selPicture;
   /**
    * Width of the image
    */
   public int width;
   /**
    * Height of the image
    */
   public int height;
   
   private boolean selected;
   /**
    * Get Bitmap Resources   
    * @param _dialog Identify the button
    * @param offImage 
    * @param onImage
    * @param w Width of the image
    * @param h Height of the image
    */
   public MyCustomButton (String name,int w,int h){
	super(Field.FIELD_HCENTER);
    try{
	    setName(name);
	    selected = false;
	    String imgName = name.toLowerCase();
	    _offPicture = Bitmap.getBitmapResource(imgName);//resizeImage(off, 301, 23).getBitmap();
	    _onPicture =  Bitmap.getBitmapResource(imgName);
	    _selPicture = _offPicture;
	    width = _offPicture.getWidth();
	    height = _offPicture.getHeight();
	    
	    _currentPicture = _offPicture;
    }catch(Exception e){
    	 // String mensaje = "Error Instanciando el Boton: "+name;
    }
   }
   /**
    * Get the Height of the button
    */
   public int getPreferredHeight(){     
    return height;
   }
   /**
    * Get the Width of the button
    */
   public int getPreferredWidth(){
    return width;
   }
   /**
    * Return if the Field is Focusable
    */
   public boolean isFocusable(){
    return true;
   }
   /**
    * Override function to switch picture 
    * @param direction
    */
   protected void onFocus(int direction){
	    _currentPicture = _onPicture;
	    invalidate();
   }
   /**
    * Override function to switch picture
    */
   protected void onUnfocus(){
	
    if(isSelected()){
		_currentPicture = _selPicture;
	}else{
		_currentPicture = _offPicture;
	}
    invalidate();
   }
   
   private boolean isSelected(){
	   return selected;
   }
   
   public void setSelect(boolean b){
	   selected = b;
	   if(isSelected()){
		   _currentPicture = _selPicture;
		    
	   }else{
		   _currentPicture = _offPicture;
	   }
	   invalidate();
   }
   /**
    * sets dimensions of layout
    * @param width
    * @param height
    */
	protected void layout(int width, int height) {
	  setExtent(Math.min( width, getPreferredWidth()), Math.min( height, getPreferredHeight()));
	}
	/**
	 * update the fieldChange
	 * @param context
	 */
	protected void fieldChangeNotify(int context) {
	    try {
	     this.getChangeListener().fieldChanged(this, context);
	    } catch (Exception exception) {
	    }
	}
	/**
	 * Since button is rounded we need to fill corners with dark color to match
	 * @param graphics
	 */
	protected void paint(Graphics graphics) { 
	 graphics.setColor(Color.ALICEBLUE); 
	       graphics.fillRect(0, 0, getWidth(), getHeight());
	       graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);
	}
	/**
	 * Listen for navigation Click
	 */
	protected boolean navigationClick(int status, int time){
	 fieldChangeNotify(1);
	 return true;
	}
	public void setName(String _name) {
		this._name = _name;
	}
	public String getName() {
		return _name;
	}

	
	
}

